// vuex

import Vue from 'vue'
import Vuex from 'vuex'
import Cookies from 'vue-cookies'

Vue.use(Vuex)


export default new Vuex.Store({
  state: {
    username:Cookies.get("username"),
    token:Cookies.get("token"),
    shopping_car_number:Cookies.get('shopping_car_number'),
    shopping_car:[],
  },
  mutations: {
    saveTokens(state,data){
      state.username = data.username;
      state.token = data.token;
      Cookies.set('username',data.username,data.expires_in);
      Cookies.set('token',data.token,data.expires_in);
    },
    clearTokens(state){
      state.username = null;
      state.token = null;
      Cookies.remove('username');
      Cookies.remove('token')
    },
    save_shopping_car_number(state,data){
      // state.shopping_car_number = data.shopping_car_number;
      var msg = state.shopping_car;
      if(msg.indexOf(data.title) === -1){
        msg.push(data.title)
      }
      var shopping_car_number = msg.length;
      state.shopping_car_number = shopping_car_number;
      Cookies.set('shopping_car_number',shopping_car_number,604800);
    },
    clear_shopping_car_number(state){
      state.shopping_car_number = 0;
    }
  }
})
